import {Injectable} from '@angular/core';
import {NzMessageService, NzNotificationService} from "ng-zorro-antd";
import {AjaxService} from "../../public/service/ajax.service";
import {SettingUrl} from "../../public/setting/setting_url";
import {HttpCodesEnum} from "../../public/setting/enums";

@Injectable({
  providedIn: 'root'
})
export class TestService {

  constructor(private notification: NzNotificationService, private message: NzMessageService) {
  }

  /**
   * 查询TestOpt列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getTestOptList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      AjaxService.get({
        url: SettingUrl.URL.testOpt.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 添加TestOpt
   * @param params
   * @returns {Promise<T>}
   */
  addTestOpt(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      AjaxService.post({
        url: SettingUrl.URL.testOpt.add,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(true);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 修改TestOpt
   * @param params
   * @returns {Promise<T>}
   */
  modifyTestOpt(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      AjaxService.put({
        url: SettingUrl.URL.testOpt.modify,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 修改TestOpt状态
   * @param code 供应商编码
   * @param state 供应商状态
   * @returns {Promise<T>}
   */
  modifyTestOptState(code, state) {
    let me = this;
    return new Promise(function (resolve, reject) {
      AjaxService.put({
        url: SettingUrl.URL.testOpt.updateState,
        data: {
          code: code,
          state: state
        },
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询TestOpt详细信息
   * @param code TestOpt编码
   * @returns {Promise<T>}
   */
  loadTestOptByCode(code) {
    let me = this;
    return new Promise(function (resolve) {
      AjaxService.get({
        url: SettingUrl.URL.testOpt.load + code,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询TestOption列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getTestOptionList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      AjaxService.get({
        url: SettingUrl.URL.testOption.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 添加TestOption
   * @param params
   * @returns {Promise<T>}
   */
  addTestOption(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      AjaxService.post({
        url: SettingUrl.URL.testOption.add,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(true);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 修改TestOption
   * @param params
   * @returns {Promise<T>}
   */
  modifyTestOption(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      AjaxService.put({
        url: SettingUrl.URL.testOption.modify,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 修改TestOption状态
   * @param code 供应商编码
   * @param state 供应商状态
   * @returns {Promise<T>}
   */
  modifyTestOptionState(code, state) {
    let me = this;
    return new Promise(function (resolve, reject) {
      AjaxService.put({
        url: SettingUrl.URL.testOption.updateState,
        data: {
          code: code,
          state: state
        },
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询TestOption详细信息
   * @param code TestOption编码
   * @returns {Promise<T>}
   */
  loadTestOptionByCode(code) {
    let me = this;
    return new Promise(function (resolve) {
      AjaxService.get({
        url: SettingUrl.URL.testOption.load + code,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询TestRecord列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getTestRecordList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      AjaxService.get({
        url: SettingUrl.URL.testRecord.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 添加TestRecord
   * @param params
   * @returns {Promise<T>}
   */
  addTestRecord(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      AjaxService.post({
        url: SettingUrl.URL.testRecord.add,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(true);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 修改TestRecord
   * @param params
   * @returns {Promise<T>}
   */
  modifyTestRecord(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      AjaxService.put({
        url: SettingUrl.URL.testRecord.modify,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 修改TestRecord状态
   * @param code 供应商编码
   * @param state 供应商状态
   * @returns {Promise<T>}
   */
  modifyTestRecordState(code, state) {
    let me = this;
    return new Promise(function (resolve, reject) {
      AjaxService.put({
        url: SettingUrl.URL.testRecord.updateState,
        data: {
          code: code,
          state: state
        },
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询TestRecord详细信息
   * @param code TestRecord编码
   * @returns {Promise<T>}
   */
  loadTestRecordByCode(code) {
    let me = this;
    return new Promise(function (resolve) {
      AjaxService.get({
        url: SettingUrl.URL.testRecord.load + code,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询TestUnit列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getTestUnitList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      AjaxService.get({
        url: SettingUrl.URL.testUnit.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 添加TestUnit
   * @param params
   * @returns {Promise<T>}
   */
  addTestUnit(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      AjaxService.post({
        url: SettingUrl.URL.testUnit.add,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(true);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 修改TestUnit
   * @param params
   * @returns {Promise<T>}
   */
  modifyTestUnit(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      AjaxService.put({
        url: SettingUrl.URL.testUnit.modify,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 修改TestUnit状态
   * @param code 供应商编码
   * @param state 供应商状态
   * @returns {Promise<T>}
   */
  modifyTestUnitState(code, state) {
    let me = this;
    return new Promise(function (resolve, reject) {
      AjaxService.put({
        url: SettingUrl.URL.testUnit.updateState,
        data: {
          code: code,
          state: state
        },
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询TestUnit详细信息
   * @param code TestUnit编码
   * @returns {Promise<T>}
   */
  loadTestUnitByCode(code) {
    let me = this;
    return new Promise(function (resolve) {
      AjaxService.get({
        url: SettingUrl.URL.testUnit.load + code,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

}
